package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

/**
 * TODO
 *
 * @author Mr.zhao
 * @version 1.0
 * @date 2022/1/4 9:18
 */
@RestController
@RequestMapping("/damageListGoods")
public class DamageListGoodsController {

    @Autowired
    private DamageListGoodsService damageListGoodsService;

    @PostMapping("/save")
    public ServiceVO save(DamageList damageList, String damageListGoodsStr,
                          HttpSession session
                         ){

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            List<DamageListGoods> readValue = objectMapper.readValue(damageListGoodsStr, new TypeReference<List<DamageListGoods>>() {
            });


            damageListGoodsService.save(damageList,session,readValue);

            return ServiceVO.Ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ServiceVO.error(e.getMessage());
        }


    }


}
